package com.lemons.hackaton.app.data;

import java.io.File;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
 






import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.lemons.hackaton.algorithm.Schedule;
import com.lemons.hackaton.algorithm.ScheduleEntry;

public class OutputWriter {
	
	public static void write(List<ScheduleEntry> schedule, Float totalMoney) {
		try {
				
				DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
				DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

				Document doc = docBuilder.newDocument();
				Element rootElement = doc.createElement("output");
				rootElement.setAttribute("xmls", "http://www.endava.com/hackathon/journey");
				rootElement.setAttribute("teamId", "lEMON");
				doc.appendChild(rootElement);
				
				Element total = doc.createElement("totalAmountSpent");
				rootElement.appendChild(total);
				
				
				for(ScheduleEntry event : schedule) {

						Element entry = doc.createElement("entry");
						rootElement.appendChild(entry);
						
						Element p = doc.createElement("personId");
						p.appendChild(doc.createTextNode(event.getPerson()));
						entry.appendChild(p);
						
						Element hour = doc.createElement("hourIndex");
						hour.appendChild(doc.createTextNode(event.getHour().toString()));
						entry.appendChild(hour);						//TODO verify if person haved activity at hourInddex

						
						if (event.getActivity() != null) {
							Element activity = doc.createElement("activity");
							activity.appendChild(doc.createTextNode(event.getActivity()));
							entry.appendChild(activity);
						}
				}
				
				
				total.appendChild(doc.createTextNode(String.valueOf(totalMoney)));
		 
				// write the content into xml file
				TransformerFactory transformerFactory = TransformerFactory.newInstance();
				Transformer transformer = transformerFactory.newTransformer();
				
				DOMSource source = new DOMSource(doc);
				StreamResult result = new StreamResult(new File("D:\\file.xml"));
				
				transformer.setOutputProperty(OutputKeys.INDENT, "yes");
				transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
				
				// Output to console for testing
				// StreamResult result = new StreamResult(System.out);
		 
				transformer.transform(source, result);
		 
		//		System.out.println("File saved!");
		 
			  } catch (ParserConfigurationException pce) {
				pce.printStackTrace();
			  } catch (TransformerException tfe) {
				tfe.printStackTrace();
			  }
	}

}
